package cn.edu.scau.cmi.tancuier.client;

import java.util.ArrayList;

import cn.edu.scau.cmi.tancuier.mutiton.Marshal;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;

public class MutitonClient extends Application {
//	String marshal[] = {"朱德", "彭德怀", "林彪", "刘伯承", "贺龙", "陈毅", "罗荣桓", "徐向前", "聂荣臻", "叶剑英"};
	
	@Override
	public void start(Stage primaryStage) throws Exception {
		ArrayList<Marshal> list = new ArrayList<Marshal>();
		primaryStage.setTitle("Multiton");
		
		FlowPane flowPane = new FlowPane();
		flowPane.setPadding(new Insets(10, 20, 10, 20));
		flowPane.setHgap(5);
		flowPane.setVgap(5);
		
		Label name = new Label("Name: ");
		flowPane.getChildren().add(name);
		
		TextField input = new TextField("");
		flowPane.getChildren().add(input);
		
		Button button = new Button("Create");
		button.setOnAction(e->{
			Label label = null;
			
			int i;
			for(i = 0; i < list.size(); i++) {
				if(list.get(i).getName().equals(input.getText())) {
					label = new Label(input.getText() + "has been created, please enter again.                              ");
					break;
				}
			}
			if(i == list.size()) {
				if (input.getText().equals("")) {
					label = new Label("Please enter the name. ");
				} else {
					Marshal marshal = Marshal.getInstance(input.getText());
					if (marshal == null) {
						label = new Label("You have already created 10 marshals.                        ");
					} else {
						list.add(marshal);
						label = new Label("The marshal that you have just created: " + marshal.getName() + ", object: " + marshal);
					}
				}
			}
			
			flowPane.getChildren().add(label);
		});
		flowPane.getChildren().add(button);
		
		Scene scene = new Scene(flowPane, 600, 300);
		primaryStage.setScene(scene);
		primaryStage.show();
	}
	
	public static void main(String[] args) {
		Application.launch(args);
	}
}
